package com.ruoyi.zhmk.service.impl;

import java.util.List;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.faceApi.click.FaceApiClick;
import com.ruoyi.faceApi.click.impl.FaceApiDefaultClick;
import com.ruoyi.faceApi.entity.FaceIdentification;
import com.ruoyi.zhmk.utils.FaceApiUtil;
import com.ylsoft.android.iacterminal.net.thrift.server.transaction.AIFTThriftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhmk.mapper.ZhmkDoorDeviceMapper;
import com.ruoyi.zhmk.domain.ZhmkDoorDevice;
import com.ruoyi.zhmk.service.IZhmkDoorDeviceService;

/**
 * 门禁设备Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-06-24
 */
@Service
public class ZhmkDoorDeviceServiceImpl implements IZhmkDoorDeviceService 
{
    @Autowired
    private ZhmkDoorDeviceMapper zhmkDoorDeviceMapper;

    /**
     * 查询门禁设备
     * 
     * @param id 门禁设备ID
     * @return 门禁设备
     */
    @Override
    public ZhmkDoorDevice selectZhmkDoorDeviceById(Long id)
    {
        return zhmkDoorDeviceMapper.selectZhmkDoorDeviceById(id);
    }

    /**
     * 查询门禁设备列表
     * 
     * @param zhmkDoorDevice 门禁设备
     * @return 门禁设备
     */
    @Override
    public List<ZhmkDoorDevice> selectZhmkDoorDeviceList(ZhmkDoorDevice zhmkDoorDevice)
    {
        List<ZhmkDoorDevice> zhmkDoorDevices = zhmkDoorDeviceMapper.selectZhmkDoorDeviceList(zhmkDoorDevice);
        for (ZhmkDoorDevice doorDevice : zhmkDoorDevices) {
            String ip = doorDevice.getIp();
            AIFTThriftService.Client client = FaceApiDefaultClick.connectMap.get(ip);
            if (client != null) {
                doorDevice.setStatus(1);
            }
        }
            return zhmkDoorDevices;
    }

    /**
     * 新增门禁设备
     * 
     * @param zhmkDoorDevice 门禁设备
     * @return 结果
     */
    @Override
    public int insertZhmkDoorDevice(ZhmkDoorDevice zhmkDoorDevice)
    {
        String ip = zhmkDoorDevice.getIp();
        String port = zhmkDoorDevice.getPort();
        if(StringUtils.isEmpty(ip) || StringUtils.isEmpty(port)){
            throw new CustomException("ip，端口为空", HttpStatus.BAD_REQUEST);
        }
        FaceApiClick faceApiClick = FaceApiUtil.getFaceApiClick();
        faceApiClick.connect(ip,Integer.parseInt(port));
        zhmkDoorDevice.setCreateTime(DateUtils.getNowDate());
        return zhmkDoorDeviceMapper.insertZhmkDoorDevice(zhmkDoorDevice);
    }

    /**
     * 修改门禁设备
     * 
     * @param zhmkDoorDevice 门禁设备
     * @return 结果
     */
    @Override
    public int updateZhmkDoorDevice(ZhmkDoorDevice zhmkDoorDevice)
    {
        String ip = zhmkDoorDevice.getIp();
        String port = zhmkDoorDevice.getPort();
        if(StringUtils.isEmpty(ip) || StringUtils.isEmpty(port)){
            throw new CustomException("ip，端口为空", HttpStatus.BAD_REQUEST);
        }
        FaceApiClick faceApiClick = FaceApiUtil.getFaceApiClick();
        faceApiClick.connect(ip,Integer.parseInt(port));
        zhmkDoorDevice.setUpdateTime(DateUtils.getNowDate());
        return zhmkDoorDeviceMapper.updateZhmkDoorDevice(zhmkDoorDevice);
    }

    /**
     * 批量删除门禁设备
     * 
     * @param ids 需要删除的门禁设备ID
     * @return 结果
     */
    @Override
    public int deleteZhmkDoorDeviceByIds(Long[] ids)
    {
        //查询出需要删除的列表
        List<ZhmkDoorDevice> zhmkDoorDevices = zhmkDoorDeviceMapper.selectZhmkDoorDeviceByIds(ids);
        FaceApiClick faceApiClick = FaceApiUtil.getFaceApiClick();
        for (ZhmkDoorDevice zhmkDoorDevice : zhmkDoorDevices) {
            String ip = zhmkDoorDevice.getIp();
            faceApiClick.closeConnect(ip);
        }
        return zhmkDoorDeviceMapper.deleteZhmkDoorDeviceByIds(ids);
    }

    /**
     * 删除门禁设备信息
     * 
     * @param id 门禁设备ID
     * @return 结果
     */
    @Override
    public int deleteZhmkDoorDeviceById(Long id)
    {
        ZhmkDoorDevice zhmkDoorDevice = zhmkDoorDeviceMapper.selectZhmkDoorDeviceById(id);
        String ip = zhmkDoorDevice.getIp();
        FaceApiClick faceApiClick = FaceApiUtil.getFaceApiClick();
        faceApiClick.closeConnect(ip);
        return zhmkDoorDeviceMapper.deleteZhmkDoorDeviceById(id);
    }


}
